package com.imelite.qyh.util;
import com.alibaba.fastjson.JSON;
import com.imelite.qyh.base.result.ResultBean;
import com.imelite.qyh.base.result.ResultBeanFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class BaseResponseUtil {
    private static final Log log = LogFactory.getLog(BaseResponseUtil.class);

    /**
     * @param @param response
     * @param @param res    响应字符串
     * @return void    返回类型
     * @throws
     * @Title: responseJson
     * @Description: JSON 格式 响应
     */
    public static void responseJson(HttpServletResponse response, String res) {
        try {
            response.setContentType("application/json;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            out.write(res);
            out.flush();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }



    /**
     * @param response
     * @param code
     * @param msg
     */
    public static void responseAjaxJson(HttpServletResponse response, String code, String msg) {
        JSONObject res = new JSONObject();
        try {
            res.put("code", code);
            res.put("msg", msg);
            responseJson(response, res.toString());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     *
     * @param response
     * @param code
     * @param msg
     * @param data
     */
    public static void responseAjaxJson(HttpServletResponse response, String code, String msg, Object data) {
        JSONObject res = new JSONObject();
        try {
            res.put("code", code);
            res.put("msg", msg);
            res.put("data", data);
            responseJson(response, res.toString());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     *
     * @param response
     * @param data
     */
    public static void responseAjaxJsonModel(HttpServletResponse response,Object data) {

        try {
            responseJson(response, JSON.toJSONString(data));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * @param response
     * @param resultBean
     */
    public static void responseAjaxJson(HttpServletResponse response, ResultBean resultBean) {
        if(resultBean==null){
            resultBean = ResultBeanFactory.getResultBeanSuccess();
        }
        responseAjaxJson(response, resultBean.getCode(), resultBean.getMsg());
    }

    /**
     * @param response
     * @param msg
     */
    public static void responseAjaxJson(HttpServletResponse response, String msg) {
        JSONObject res = new JSONObject();
        try {
            res.put("msg", msg);
            responseJson(response, res.toString());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
    
    public static void responseAjaxJsonStatus(HttpServletResponse response, ResultBean resultBean) {
    	JSONObject res = new JSONObject();
    	try {
    		res.put("status", resultBean.getCode());
    		res.put("msg", resultBean.getMsg());
    		responseJson(response, res.toString());
    	} catch (Exception e) {
    		log.error(e.getMessage(), e);
    	}
    }
}
